<template>
	<view class='contents'>
		<view class="body">
			<uni-forms :value="formData" :rules="rules" ref="form">
				<uni-group>
					
					<view class="">
						<text>你可能需要提交的材料有：\n</text>
						<text style="color: #DD524D;">1.户口本户主页
							2.本人身份证
							3.家庭成员户口本
							
							{{material}}
							N.其他\n
						</text>
					</view>

					<uni-forms-item label="证明材料自查" name="materials" required labelWidth="200" labelPosition="top">
						<text style="color: #DD524D;font-size: 16px;">
							请选择或填写您所提交的证明材料名称\n
						</text>
						<uni-data-checkbox multiple v-model="formData.materials" :localdata="materialData">
						</uni-data-checkbox>
					</uni-forms-item>
					
					<uni-forms-item label="" name="other_material">
						<uni-easyinput type="textarea" v-model="formData.other_material" placeholder="其他证明">
						</uni-easyinput>
					</uni-forms-item>

					<uni-forms-item label="证明材料上传" required labelWidth="200" name="" labelPosition="top">
						<uni-file-picker style="text-align: center;" limit="50" fileMediatype="image" mode="grid"
							@success="success" @delete="delete_file" />
					</uni-forms-item>



				</uni-group>

				<uni-group>
					<uni-forms-item label="学生本人签名" required labelWidth="120" name="" labelPosition="top">
						<view class="qm">
							<view class="qm_btn">
								<button type="primary" @click="state=startgraph('stu')">签名</button>
							</view>
						</view>
					</uni-forms-item>

				</uni-group>


				<uni-group>
					<uni-forms-item label="学生家长或监护人签名" required labelWidth="200" name="" labelPosition="top">
						<view class="qm">
							<view class="qm_btn">
								<button type="primary" @click="startgraph('par')">签名</button>
							</view>
						</view>
					</uni-forms-item>
					
				</uni-group>

				<uni-group>
					<uni-forms-item label="家庭信息采集" required labelWidth="200" name="" labelPosition="top">
						<view class="qm">
							<view class="qm_btn">
								<button type="primary" @click="startCatch">开始采集</button>
							</view>
						</view>
					</uni-forms-item>
					
				</uni-group>

				<uni-group title=" " margin-top="">
					<view class="warn">
						<text style="margin-left: 20px;">请保证所填写资料真实、准确，并同意授权相关部门通过信息核对，对所填资料进行查询、核对。</text>
					</view>

					<view class="warn">
						<text style="margin-left: 20px;color: #DD524D;">如虚报材料，本人愿意承担相关责任</text>
					</view>

					<view class="submit_btn">
						<button type="primary" @click="sub">确认提交</button>
					</view>
				</uni-group>
			</uni-forms>





		</view>


	</view>
</template>

<script>
	var touchs = [];
	var canvasw = 0;
	var canvash = 0;
	var _that;
	var testNode;
	var form;

	Date.prototype.format = function(fmt) {
		var o = {
			"M+": this.getMonth() + 1, //月份
			"d+": this.getDate(), //日
			"h+": this.getHours(), //小时
			"m+": this.getMinutes(), //分
			"s+": this.getSeconds(), //秒
			"q+": Math.floor((this.getMonth() + 3) / 3), //季度
			"S": this.getMilliseconds() //毫秒
		};

		if (/(y+)/.test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
		}

		for (var k in o) {
			if (new RegExp("(" + k + ")").test(fmt)) {
				fmt = fmt.replace(
					RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
			}
		}

		return fmt;
	}

	//获取系统信息
	uni.getSystemInfo({
		success: function(res) {
			canvasw = res.windowWidth;
			canvash = res.windowHeight;
		},
	})
	export default {
		data() {
			return {
				imageValue: [],
				stu_id: '',
				signImage: '',
				calcel_text: '取消',
				autograph_btn_stu: '签名',
				autograph_btn_par: '签名',
				isEnd: false,
				stu: false,
				par: false,
				status: '',
				autograph: [],
				FilePath: [],
				material: '',
				bool: false,
				ctx: null,
				formData: {
					materials: [],
					other_material: ''
				},
				rules: {
					materials: {
						rules: [{
							required: true,
							errorMessage: '请选择您所有提交材料的名称'
						}]
					}
				},
				materialData: [{
						text: '原建档立卡脱贫家庭证明',
						value: 0
					},
					{
						text: '五保证',
						value: 1
					},
					{
						text: '特困人员救助供养证',
						value: 2
					},
					{
						text: '儿童福利证',
						value: 3
					},
					{
						text: '低保证',
						value: 4
					},
					{
						text: '特困职工证',
						value: 5
					},
					{
						text: '救助证',
						value: 6
					},
					{
						text: '低收入证',
						value: 7
					},
					{
						text: '优抚对象证明',
						value: 8
					},
					{
						text: '因公牺牲警察证明',
						value: 9
					},
					{
						text: '残疾人证',
						value: 10
					},
					{
						text: '重大疾病的医生证明(盖医院公章方有效)',
						value: 11
					},
					{
						text: '受灾证明(政府部门开具并盖公章)',
						value: 12
					},
					{
						text: '家庭成员在校证明',
						value: 13
					},
					{
						text: '家庭成员户口本',
						value: 14
					}
				]
			}
		},
		methods: {

			sub() {
				const that = this
				let autograph = ''
				this.$refs.form.validate().then(res => {
		
					if (this.FilePath.length != 0) {
						if(getApp().globalData.stuAutographStatus == false){
							uni.showToast({
								icon:'none',
								title:'学生本人未签名'
							})
							return
						}
						if(getApp().globalData.parAutographStatus == false){
							uni.showToast({
								icon:'none',
								title:'学生家长未签名'
							})
							return
						}
						if(getApp().globalData.catch == false){
							uni.showToast({
								icon:'none',
								title:'家庭信息未采集'
							})
							return
						}
						uni.showModal({
							title: '提示',
							content: '是否确认提交？请务必确保数据准确，这将影响最终评定结果',
							success: function(r) {
								if (r.confirm) {

									uni.showLoading({
										title: '正在提交,请稍后'
									})

									uni.getStorage({
										key: 'kunnan_info',
										success: function(e) {
											const db = uniCloud.database();
											db.collection('kunnan_info')
												.where({
													stu_id: e.data.kunnan.stu_id
												})
												.count()
												.then(res => {
													//判断是否已提交过
													// console.log(res.result)
													if (res.result.total == 0) {
														// console.log(res.result)
														//新增
														var date = new Date()
														db.collection('kunnan_info')
															.add({

																stu_id: e.data
																	.kunnan.stu_id,
																name: e.data.kunnan
																	.name,
																content: e.data,
																autograph: autograph,
																FilePath: that
																	.FilePath,
																date: date.format(
																	'yyyy-MM-dd hh:mm:ss'
																)

															}).then((res) => {
																uni.hideLoading()
																uni.navigateTo({
																	url: '../success/success?res=请保存佐证材料纸质版,下学期提供.&tips=提交成功'
																})
															}).catch((err) => {
																uni.hideLoading()
																uni.showModal({
																	content: err
																		.message ||
																		'新增失败',
																	showCancel: false
																})

															})

													} else {
														//更新
														uni.hideLoading()
														uni.showModal({
															title: '提示',
															content: '您已提交过申请,是否再次提交?本操作将覆盖上次提交数据包括证明文件',
															success: (res) => {
																if (res
																	.confirm
																) {
																	uni.showLoading({
																		title: '正在提交,请稍后'
																	})
																	var date =
																		new Date()
																	db.collection(
																			"kunnan_info"
																		)
																		.where({
																			stu_id: e
																				.data
																				.kunnan
																				.stu_id
																		})
																		.update({
																			name: e
																				.data
																				.kunnan
																				.name,
																			content: e
																				.data,
																			autograph: autograph,
																			FilePath: that
																				.FilePath,
																			date: date
																				.format(
																					'yyyy-MM-dd hh:mm:ss'
																				)
																		})
																		.then(
																			(
																				res
																			) => {
																				uni.hideLoading()
																				uni.navigateTo({
																					url: '../success/success?res=请保存佐证材料纸质版,下学期提供.&tips=修改成功'
																				})

																			}
																		)
																		.catch(
																			(
																				err
																			) => {
																				uni.hideLoading()
																				uni.showModal({
																					content: err
																						.message ||
																						'更新失败',
																					showCancel: false
																				})
																			}
																			)
																}
															}
														})

													}
												}).catch(err => {
													console.error(err)
													uni.hideLoading()
												})
										},
										fail: function() {
											console.log('失败')
											uni.hideLoading()
										}
									})
								}
							}
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: '证明文件未上传'
						})
					}

				}).catch(err => {
					// 表单错误提示
					// console.log(err)
					uni.showModal({
						title: '提示',
						content: err[0].errorMessage,
						showCancel: false
					})
				})

			},
			success(e) {
				this.FilePath.push(e.tempFilePaths[0])
				uni.showToast({
					icon: 'none',
					title: '上传成功'
				})
			},
			delete_file(e) {
				// console.log(e)

				this.FilePath.splice(this.FilePath.indexOf(e.tempFilePath), 1)
				
			},
			startgraph(i) {
				uni.navigateTo({
					url: '../am/am?index=' + i + '&stu_id=' + this.stu_id
				})
			},
			startCatch(){
				if(this.stu_id == ''){
					uni.showModal({
						title:'提示',
						content:'参数错误'
					})
					return
				}
				uni.navigateTo({
					url:'../catch/catch?stu_id='+this.stu_id
				})
			}
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			uni.showLoading({
				title: '加载中'
			})
			uni.getStorage({
				key: 'kunnan_info',
				success: (res) => {
					this.stu_id = res.data.kunnan.stu_id
					let i = 0
					if (res.data.jbqk != undefined && res) {

						for (i; i < res.data.jbqk.type.length; i++) {

							switch (res.data.jbqk.type[i]) {
								case 0:
									// console.log(res.data.jbqk.type[0])
									this.material = this.material.concat(4 + i +
										'.建档立卡证明(扶贫手册、精准扶贫...手册)\n')
									break
								case 1:
									this.material = this.material.concat(4 + i + '.特困供养人员证明\n')
									break
								case 2:
									this.material = this.material.concat(4 + i + '.低保证\n')
									break
								case 3:
									this.material = this.material.concat(4 + i + '.特困职工证\n')
									break
								case 4:
									this.material = this.material.concat(4 + i + '.低收入证\n')
									break
								case 5:
									this.material = this.material.concat(4 + i + '.孤儿证明(儿童福利证...)\n')
									break
								case 6:
									this.material = this.material.concat(4 + i + '.父母不能履行抚养义务证明\n')
									break
								case 7:
									this.material = this.material.concat(4 + i + '.父母一方抚养证明\n')
									break
								case 8:
									this.material = this.material.concat(4 + i + '.优抚对象证明\n')
									break
								case 9:
									this.material = this.material.concat(4 + i + '.因公牺牲警察子女证明\n')
									break
							}
							if (res.data.jbqk.hea.length != 0) {
								this.material = this.material.concat(4 + i++ + '.本人残疾证\n')

							}
							if (res.data.jbqk.diseases.length != 0) {
								this.material = this.material.concat(4 + i++ + '.本人患重大疾病证明\n')

							}
						}
						if (res.data.income != undefined) {
							if (res.data.income.Emergency != undefined) {
								if (res.data.income.Emergency.length != 0) {
									this.material = this.material.concat(4 + i++ +
										'.家庭突发意外事件证明,两年以内重大自然灾害的请提供相应的电视报道截图或县级以上证明\n')
								}


							}
						}
					}

					this.material = this.material.concat(4 + i + '.其他证明\n')
					uni.hideLoading();
					setTimeout(function() {

					}, 300);
				},
				fail: (err) => {
					setTimeout(function() {
						uni.hideLoading();
					}, 300);
				}
			})
		},
	}
</script>

<style>
	.ts {
		color: #FF485D;
		font-size: 30upx;
		height: 100upx;
		line-height: 100upx;
		padding-left: 20upx;
	}

	canvas {
		background-color: #fff;
		width: 100vw;
		max-width: 620px;
		/* margin: 0 20rpx; */
		height: 30vh;
		border-radius: 5px;
	}

	.contents {
		/* padding-top: 20upx;
		padding-bottom: 100upx;
		box-sizing: border-box; */
	}

	#signatureImg {
		background-color: #EEEEEE;
	}

	.caozuo {
		display: flex;
		height: 100upx;
		width: 750upx;
		position: fixed;
		left: 0;
		bottom: 0;
	}

	.caozuo view {
		width: 375upx;
		text-align: center;
		height: 100upx;
		line-height: 100upx;
		color: #FFFFFF;
	}

	.caozuo view:active {
		background-color: #CCCCCC;
		color: #333333;
	}

	.chongqian {
		background-color: #FF8F58;
	}

	.over {
		background-color: #0599D7;
	}

	.qm_btn button {
		font-size: 15px;
		width: 250rpx;
		margin-top: 15px;
	}

	.pop_body {
		/* height: 100vh; */
		padding: 5px;

	}

	.submit_btn button {
		font-size: 15px;
		width: 250rpx;
		margin-top: 15px;
	}

	.warn {
		font-size: 13px
	}

	.can {
		display: flex;
		justify-content: center;
	}

	.jSignature {
		margin: 10px 0px;
		display: flex;
		border: 1px dashed #aaa;
		/* max-width: 320px; */
		width: 100vw;
		height: 160px;
		/* max-height: 426px; */
		max-width: 380px;
		touch-action: none;
		background-color: rgb(246, 246, 246);
		display: block;
		outline: none;
	}
</style>
